/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.skill.executor;

import com.lineage.server.PacketCreate;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.L1Character;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.Instance.L1SummonInstance;
import com.lineage.server.model.skill.L1SkillExecutor;
import com.lineage.server.templates.L1Npc;
import com.lineage.server.utils.Random;

/**
 * Class <code>SummonLesserElemental</code> 精灵魔法:召唤属性精灵 (20/0/2).
 * 召唤自身学习之属性的精灵之后，在持续时间内可使用它。但在此期间无法召唤其他现有的精灵。（妖精30级试炼取得。）
 * 
 * @author jrwz
 * @version 2012-7-24下午03:04:41
 * @see com.lineage.server.model.skill.executor
 * @since JDK1.7
 */
public final class SummonLesserElemental extends L1SkillExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static L1SkillExecutor get() {
        return new SummonLesserElemental();
    }

    /** 精灵魔法:召唤属性精灵 (20/0/2). */
    private SummonLesserElemental() {
    }

    @Override
    public void execute(final L1Character user, final L1Character cha,
            final L1Character target, final int targetID, final int skillId,
            final int getBuffIconDuration) {
        if (user instanceof L1PcInstance) {
            final L1PcInstance pc = (L1PcInstance) user;
            final int attr = pc.getElfAttr();
            if (attr != 0) { // 检查有无属性(是否定系)
                if (pc.getMap().isRecallPets()) {
                    int petcost = 0;
                    for (final L1NpcInstance pet : pc.getPetList().values()) {
                        petcost += pet.getPetcost(); // 当前宠物的cost
                    }

                    if (petcost == 0) { // 只能召唤一只
                        int summonid = 0;
                        final int[] summons = new int[] { 45306, 45303, 45304,
                                45305 };
                        int npcattr = 1;
                        for (int i = 0; i < summons.length; i++) {
                            if (npcattr == attr) {
                                summonid = summons[i];
                                i = summons.length;
                            }
                            npcattr *= 2;
                        }
                        // 设定特殊的场合随机出现
                        if (summonid == 0) {
                            final int k3 = Random.nextInt(4);
                            summonid = summons[k3];
                        }
                        final L1Npc npcTemp = NpcTable.getInstance()
                                .getTemplate(summonid);
                        final L1SummonInstance summon = new L1SummonInstance(
                                npcTemp, pc);
                        summon.setPetcost(pc.getCha() + 7); // 其他的宠物
                    }
                } else {
                    PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
                }
            } else {
                PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
            }
        }
    }

    @Override
    public void stop(final L1Character cha, final int skillId) {
        // TODO Auto-generated method stub
    }
}
